package cn.csc.module.pay.controller.admin.drawcash;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigInteger;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.pay.controller.admin.drawcash.vo.*;
import cn.csc.module.pay.dal.dataobject.drawcash.PayDrawCashDO;
import cn.csc.module.pay.convert.drawcash.PayDrawCashConvert;
import cn.csc.module.pay.service.drawcash.PayDrawCashService;

@Api(tags = "管理后台 - 提款申请记录")
@RestController
@RequestMapping("/pay/draw-cash")
@Validated
public class PayDrawCashController {

    @Resource
    private PayDrawCashService payDrawCashService;

    @PostMapping("/create")
    @ApiOperation("创建提款申请记录")
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:create')")
    public CommonResult<BigInteger> createDrawCash(@Valid @RequestBody PayDrawCashCreateReqVO createReqVO) {
        return success(payDrawCashService.createDrawCash(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新提款申请记录")
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:update')")
    public CommonResult<Boolean> updateDrawCash(@Valid @RequestBody PayDrawCashUpdateReqVO updateReqVO) {
        payDrawCashService.updateDrawCash(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除提款申请记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:delete')")
    public CommonResult<Boolean> deleteDrawCash(@RequestParam("id") BigInteger id) {
        payDrawCashService.deleteDrawCash(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得提款申请记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:query')")
    public CommonResult<PayDrawCashRespVO> getDrawCash(@RequestParam("id") BigInteger id) {
        PayDrawCashDO drawCash = payDrawCashService.getDrawCash(id);
        return success(PayDrawCashConvert.INSTANCE.convert(drawCash));
    }

    @GetMapping("/list")
    @ApiOperation("获得提款申请记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:query')")
    public CommonResult<List<PayDrawCashRespVO>> getDrawCashList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<PayDrawCashDO> list = payDrawCashService.getDrawCashList(ids);
        return success(PayDrawCashConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得提款申请记录分页")
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:query')")
    public CommonResult<PageResult<PayDrawCashRespVO>> getDrawCashPage(@Valid PayDrawCashPageReqVO pageVO) {
        PageResult<PayDrawCashDO> pageResult = payDrawCashService.getDrawCashPage(pageVO);
        return success(PayDrawCashConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出提款申请记录 Excel")
    @PreAuthorize("@ss.hasPermission('pay:draw-cash:export')")
    @OperateLog(type = EXPORT)
    public void exportDrawCashExcel(@Valid PayDrawCashExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayDrawCashDO> list = payDrawCashService.getDrawCashList(exportReqVO);
        // 导出 Excel
        List<PayDrawCashExcelVO> datas = PayDrawCashConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "提款申请记录.xls", "数据", PayDrawCashExcelVO.class, datas);
    }

}
